package com.free.company.member.controller;

import com.free.common.vo.ResultModel;
import com.free.company.member.service.AuthInfoService;
import com.free.company.member.service.JsTicketService;
import com.free.ext.logger.FreeLogger;
import com.free.ext.logger.FreeLoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import java.util.Map;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@Controller
public class AuthInfoController {
    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    FreeLogger LOGGER = FreeLoggerFactory.getLogger(AuthInfoController.class);

    @Autowired
    private AuthInfoService service;

    @Autowired
    private JsTicketService ticketService;

    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/
    @RequestMapping("/authInfo")
    public String authInfo(String code, String appid, String extattr, String url, RedirectAttributesModelMap redirectAttrs){
        LOGGER.info("拿到code:{},appid:{},extattr:{},jumpUrl:{}",code,appid,extattr,url);
//        String redirectUrl = service.authInfo(code,appid,extattr,url);
//        if(StringUtils.isEmpty(redirectUrl)){
//            return "redirect:" + url;
//        }else{
//            return "redirect:" + redirectUrl;
//        }
        Map<String,String> redirectParams = service.authInfoParams(code, appid, extattr, url);
        redirectAttrs.addAllAttributes(redirectParams);
        return "redirect:" + url;
    }

    @RequestMapping("/auth")
    public String auth(String appid, String extattr, String url){
        String redirectUrl = service.getRedirectUrl(appid, extattr, url);
        return "redirect:" + redirectUrl;
    }

    @RequestMapping("/jsticket")
    @ResponseBody
    public ResultModel jsTicket(String url){
        return ticketService.getSignString(url);
    }

    @RequestMapping("/authwelcome")
    public String authWelcome(){
        return "loading";
    }

    @RequestMapping("/ajaxAuthInfo")
    @ResponseBody
    public ResultModel authInfoRes(String code,String appid, String extattr, String url){
        LOGGER.info("页面中转拿到code:{},appid:{},extattr:{},jumpUrl:{}",code,appid,extattr,url);
        String redirectUrl = service.authInfo(code,appid,extattr,url);
        if(StringUtils.isEmpty(redirectUrl)){
            return ResultModel.success(url);
        }else{
            return ResultModel.success(redirectUrl);
        }
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
